package com.maixy.dailydemo.export;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ReturnGoodsOrderExport
 * @Description TODO
 * @Date 2022/2/24 17:33
 * @Author NINGMEI
 */
public class ReturnGoodsOrderExport {
    public static void main(String[] args) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("退货单");
        short width = 11;
        sheet.setDefaultColumnWidth(width);

        //设置为居中加粗
        CellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        //下边框
        style.setBorderBottom(BorderStyle.THIN);
        //左边框
        style.setBorderLeft(BorderStyle.THIN);
        //上边框
        style.setBorderTop(BorderStyle.THIN);
        //右边框
        style.setBorderRight(BorderStyle.THIN);
        //水平居中
        style.setAlignment(HorizontalAlignment.CENTER);
        //上下居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置自动换行
        style.setWrapText(true);


        CellRangeAddress cra1 = new CellRangeAddress(0, 0, 0, 7);
        sheet.addMergedRegion(cra1);
        HSSFRow row0 = sheet.createRow(0);
        row0.setHeight((short) 400);
        row0.createCell(0).setCellValue("退货单");
        RegionUtil.setBorderBottom(BorderStyle.THIN, cra1, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cra1, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cra1, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cra1, sheet);
        row0.getCell(0).setCellStyle(style);

        sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 7));
        HSSFRow row1 = sheet.createRow(1);
        row1.setHeight((short) 400);
        row1.createCell(0).setCellValue("退货原因");
        row1.getCell(0).setCellStyle(style);
        row1.createCell(1).setCellValue("客户反馈需要七只，有一件是6只，要求退货");
        row1.getCell(1).setCellStyle(style);


        sheet.addMergedRegion(new CellRangeAddress(2, 2, 1, 4));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 7));
        HSSFRow row2 = sheet.createRow(2);
        row2.setHeight((short) 400);
        //test(row2, style);
        row2.createCell(0).setCellValue("日期");
        row2.getCell(0).setCellStyle(style);
        row2.createCell(1).setCellValue("2020-10-11");
        row2.getCell(1).setCellStyle(style);
        row2.createCell(5).setCellValue("取货司机");
        row2.getCell(5).setCellStyle(style);
        row2.createCell(6).setCellValue("陈志伟");
        row2.getCell(6).setCellStyle(style);

        sheet.addMergedRegion(new CellRangeAddress(3, 3, 3, 4));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 6, 7));
        HSSFRow row3 = sheet.createRow(3);
        row3.setHeight((short) 400);
        //test(row3, style);
        row3.createCell(0).setCellValue("客户姓名");
        row3.getCell(0).setCellStyle(style);
        row3.createCell(1).setCellValue("梁小姐");
        row3.getCell(1).setCellStyle(style);
        row3.createCell(2).setCellValue("客户电话");
        row3.getCell(2).setCellStyle(style);
        row3.createCell(3).setCellValue("13124992567");
        row3.getCell(3).setCellStyle(style);
        row3.createCell(5).setCellValue("商家名");
        row3.getCell(5).setCellStyle(style);
        row3.createCell(6).setCellValue("金牌窖鸡");
        row3.getCell(6).setCellStyle(style);

        sheet.addMergedRegion(new CellRangeAddress(4, 4, 1, 7));
        HSSFRow row4 = sheet.createRow(4);
        row4.setHeight((short) 800);
        //test(row4, style);
        row4.createCell(0).setCellValue("客户地址");
        row4.getCell(0).setCellStyle(style);
        row4.getCell(0).setCellStyle(style);
        row4.createCell(1).setCellValue("广东省湛江市");
        row4.getCell(1).setCellStyle(style);

        sheet.addMergedRegion(new CellRangeAddress(5, 5, 0, 4));
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 5, 7));
        HSSFRow row5 = sheet.createRow(5);
        row5.setHeight((short) 400);
        //test(row5, style);
        row5.createCell(0).setCellValue("商品名称");
        row5.getCell(0).setCellStyle(style);
        row5.createCell(5).setCellValue("数量");
        row5.getCell(5).setCellStyle(style);

        List<Activity> activityList = new ArrayList<>();
        activityList.add(new Activity("1","2","3","1","中德德久三黄鸡 旺角七只"));
        activityList.add(new Activity("11","22","33","2","中德德久三黄鸡 旺角六只"));
        int index = 6;
        for (Activity activity : activityList) {
            sheet.addMergedRegion(new CellRangeAddress(index, index, 0, 4));
            sheet.addMergedRegion(new CellRangeAddress(index, index, 5, 7));
            HSSFRow row = sheet.createRow(index);
            row.setHeight((short) 400);
            //test(row, style);
            row.createCell(0).setCellValue(activity.getValue5());
            row.getCell(0).setCellStyle(style);
            row.createCell(5).setCellValue(activity.getValue4());
            row.getCell(5).setCellStyle(style);
            index += 1;
        }

        sheet.addMergedRegion(new CellRangeAddress(index, index, 0, 5));
        sheet.addMergedRegion(new CellRangeAddress(index, index, 6, 7));
        HSSFRow lastRow = sheet.createRow(index);
        lastRow.setHeight((short) 400);
        //test(lastRow, style);
        lastRow.createCell(0).setCellValue("注意：请确认并查看包装是否完好，是否解冻并核查重量，顺路拉回");
        lastRow.getCell(0).setCellStyle(style);
        lastRow.createCell(6).setCellValue("此单为取货联");
        lastRow.getCell(6).setCellStyle(style);

        writeAndClose("sss.xlsx", workbook);
    }

    private static void writeAndClose(String fileName, Workbook wb) {
        File file;
        FileOutputStream stream = null;
        try {
            file = new File("C:\\Users\\NINGMEI\\Desktop\\");
            stream = new FileOutputStream(new File(file, fileName));
            wb.write(stream);
            //wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        }
    }

    private static void test(HSSFRow row, CellStyle style) {
        row.getCell(0).setCellStyle(style);
        row.getCell(1).setCellStyle(style);
        row.getCell(2).setCellStyle(style);
        row.getCell(3).setCellStyle(style);
        row.getCell(4).setCellStyle(style);
        row.getCell(5).setCellStyle(style);
        row.getCell(6).setCellStyle(style);
        row.getCell(7).setCellStyle(style);
    }
}
